package com.tao.service.impl;

import com.tao.context.UserContext;
import com.tao.mapper.ProductMapper;
import com.tao.mapper.SetmealMapper;
import com.tao.mapper.ShoppingCartMapper;
import com.tao.pojo.dto.CleanShoppingCartDto;
import com.tao.pojo.dto.ShoppingCartDto;
import com.tao.pojo.entity.ShoppingCart;
import com.tao.pojo.vo.ProductVo;
import com.tao.pojo.vo.SetmealByIdVo;
import com.tao.pojo.vo.SetmealVo;
import com.tao.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class ShoppingCartService1 implements ShoppingCartService {
    @Autowired
    ShoppingCartMapper shoppingCartMapper;
    @Autowired
    SetmealMapper setmealMapper;
    @Autowired
    ProductMapper productMapper;

    @Override
    public List<ShoppingCart> selectShoppingCart() {
        List<ShoppingCart> list = shoppingCartMapper.getAllByUserId(UserContext.getThreadUserId());
        log.info("查询用户:{}的购物车数据为:{}", UserContext.getThreadUserId(), list);
        return list;
    }

    @Override
    public void addShoppingCart(ShoppingCartDto shoppingCartDto) {
        log.info("接收的数据为:{}", shoppingCartDto);
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDto, shoppingCart);

        //一些公共数据无需判断可提前赋值
        shoppingCart.setUserId(UserContext.getThreadUserId());
        shoppingCart.setCreateTime(LocalDateTime.now());
        log.info("未进行判断前的shoppingcart:{}", shoppingCart);

        //注意：必须用shoppingcart判断是否存在，dto没有userId！！！！！
        ShoppingCart shoppingCart1 = shoppingCartMapper.getShoppingCart(shoppingCart);

        if (shoppingCart1 != null) {
            log.info("购物车中已存在该数据，修改数量即可");
            shoppingCart1.setNumber(shoppingCart1.getNumber() + 1);
            shoppingCartMapper.updateNumber(shoppingCart1);
        } else {
            log.info("购物车中不存在该数据，需要添加");
            shoppingCart.setNumber(1);//初始未传number，要进行修改,无论进行那种判断number都为1，提前赋值

            if (shoppingCartDto.getProductId() != null) {
                log.info("添加的为产品");
                ProductVo productVo = productMapper.getByIdMapper(shoppingCart.getProductId());
                shoppingCart.setName(productVo.getName());
                shoppingCart.setImage(productVo.getImage());
                shoppingCart.setPrice(productVo.getPrice());
//                shoppingCartMapper.addShoppingCart(shoppingCart);
//                log.info("成功添加产品:{}", shoppingCart);
            } else {
                log.info("添加的为套餐");
                SetmealByIdVo setmealVo = setmealMapper.getById(shoppingCart.getSetmealId());
                shoppingCart.setName(setmealVo.getName());
                shoppingCart.setImage(setmealVo.getImage());
                shoppingCart.setPrice(setmealVo.getPrice());
//                shoppingCartMapper.addShoppingCart(shoppingCart);
//                log.info("成功添加套餐:{}", shoppingCart);
//                统一步骤可分离
            }
            shoppingCartMapper.addShoppingCart(shoppingCart);
            log.info("成功添加购物车:{}", shoppingCart);
        }
    }

    @Override
    public void subShoppingCart(ShoppingCartDto shoppingCartDto) {
        log.info("接收的数据为:{}", shoppingCartDto);
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDto, shoppingCart);

        shoppingCart.setUserId(UserContext.getThreadUserId());
        //赋值userId后才能进行查询
        log.info("查询条件:{}",shoppingCart);
        ShoppingCart shoppingCart1 = shoppingCartMapper.getShoppingCart(shoppingCart);
         if(shoppingCart1.getNumber()>1){
             shoppingCart1.setNumber(shoppingCart1.getNumber() - 1);
         }else {
             shoppingCartMapper.cleanShoppingCart(CleanShoppingCartDto.builder().userId(UserContext.getThreadUserId()).productId(shoppingCart1.getProductId()).setmealId(shoppingCart1.getSetmealId()).build());
         }

        shoppingCartMapper.updateNumber(shoppingCart1);
        log.info("删除一个商品后的数据:{}", shoppingCart1);
    }

    @Override
    public void cleanShoppingCart() {
        shoppingCartMapper.cleanShoppingCart(CleanShoppingCartDto.builder().userId(UserContext.getThreadUserId()).build());
    }
}
